package cn.usian.service.impl;

import cn.usian.common.ResultEnum;
import cn.usian.feign.UserFeign;
import cn.usian.mapper.LesseeMapper;
import cn.usian.pojo.Lessee;
import cn.usian.common.Results;
import cn.usian.service.LesseeService;
import cn.usian.threadlocal.AuthThreadLocal;
import cn.usian.vo.LesseeVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.DeleteMapping;

import java.time.LocalDateTime;

@Service
public class LesseeServiceImpl extends ServiceImpl<LesseeMapper, Lessee> implements LesseeService {
    @Autowired
    LesseeMapper lesseeVos;
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    UserFeign userFeign;

    @Override
    public Results findPage(Integer pageNum, Integer pageSize,Lessee lessee) {
        Page<LesseeVo> page = new Page<>(pageNum, pageSize);
        page = lesseeVos.findPage(page,lessee);
        return Results.build(page, ResultEnum.SUCCESS);
    }

    @Override
    public Lessee findById(Long id) {
        Lessee lessee = this.baseMapper.selectById(id);
        return lessee;
    }

    @Override
    public Results addOrUpdate(Lessee lessee) {
        if (lessee.getId()==null){
            lessee.setStatus(1);
            Long id = AuthThreadLocal.get().getId();
            lessee.setUid(Integer.parseInt(id + ""));
            lessee.setUserPhone(userFeign.findUserName(lessee.getLesseeUser()));
            lessee.setIsDeleted(0);
            lessee.setCreateUser(AuthThreadLocal.get().getUsername());
            lessee.setCreateTime(LocalDateTime.now());
            this.baseMapper.insert(lessee);
        }else {
            lessee.setUpdateUser(AuthThreadLocal.get().getUsername());
            lessee.setUpdateTime(LocalDateTime.now());
            this.baseMapper.updateById(lessee);
        }
        return Results.build(null,ResultEnum.SUCCESS);
    }


}
